package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	/**
	 * ...
	 * @author hbb
	 */
	public class URLLoaderTest extends Sprite 
	{
		
		public function URLLoaderTest() 
		{
			//testProgressDataText();
			testProgressDataBinary();
		}
		
		
		// about reason of ProgressEvent.byteTotals=0
		// because of dynamic generated content
		// more see:
		// http://www.sephiroth.it/weblog/archives/2004/06/ho_to_get_bytestotal_from_a_dynamic_g.php
		
		private function testProgressDataText():void
		{
			// URLLoader.data is undefined while progress
			
			var ldr:URLLoader = new URLLoader;
			ldr.dataFormat = URLLoaderDataFormat.TEXT;
			ldr.addEventListener(Event.OPEN, open);
			ldr.addEventListener(ProgressEvent.PROGRESS, progress);
			ldr.addEventListener(Event.COMPLETE, complete);
			ldr.load( new URLRequest('http://s.163.com') );
			
			function progress(e:ProgressEvent):void
			{
				trace(e);
				trace('data:', ldr.data);
			}
			function complete(e:Event):void
			{
				trace(e);
				trace('data:', ldr.data.substr(0,30) , '...', ldr.data.substr(-30));
			}
			function open(e:Event):void
			{
				trace(e);
			}
		}
		
		private function testProgressDataBinary():void
		{
			// URLLoader.data is undefined while progress even if binary
			
			var ldr:URLLoader = new URLLoader;
			ldr.dataFormat = URLLoaderDataFormat.BINARY;
			ldr.addEventListener(Event.OPEN, open);
			ldr.addEventListener(ProgressEvent.PROGRESS, progress);
			ldr.addEventListener(Event.COMPLETE, complete);
			ldr.load( new URLRequest('http://www.autoplaymediastudio.com/upload_files/download_file_56.zip') );
			
			function progress(e:ProgressEvent):void
			{
				trace(e);
				trace('data:', ldr.data);
			}
			function complete(e:Event):void
			{
				trace(e);
				trace('data:', ByteArray(ldr.data).bytesAvailable);
			}
			function open(e:Event):void
			{
				trace(e);
			}
		}
		
	}

}